package com.sibu.yx.promotion.persistence.model.sys;

import java.util.HashSet;
import java.util.Set;

import com.doubo.common.util.StringUtil;

import lombok.Getter;
import lombok.Setter;

/**
 * @author suzheng
 * @date 2021-07-08
 **/
public class SysRole {
    /**
     * 角色id
     */
    @Getter
    @Setter
    private Long id;

    /**
     * 角色名称
     */
    @Getter
    @Setter
    private String name;

    /**
     * 中文名称
     */
    @Getter
    @Setter
    private String cnname;

    /**
     * 资源列表
     */
    @Getter
    @Setter
    private String resourceIds;

    /**
     * 是否有效：1有效 0无效（是否可用,如果不可用将不会添加给用户）
     */
    @Getter
    @Setter
    private Boolean available = Boolean.FALSE;

    /**
     * 拥有的资源集合
     */
    private Set<Long> resourceIdsSet;

    public SysRole() {
    }

    public SysRole(String name, String cnname, Boolean available) {
        this.name = name;
        this.cnname = cnname;
        this.available = available;
    }

    public Set<Long> getResourceIdsSet() {
        if (resourceIdsSet != null) {
            return resourceIdsSet;
        }
        resourceIdsSet = new HashSet<>();
        if (resourceIds != null) {
            String[] dbroleIds = StringUtil.trim(resourceIds, ",").split(",");
            for (String dbroleId : dbroleIds) {
                if (StringUtil.isNumeric(dbroleId)) {
                    resourceIdsSet.add(Long.valueOf(dbroleId));
                }
            }
        }
        return resourceIdsSet;
    }
}
